/**
 * 
 */
package com.epart.widget.util;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 기타 유틸리티 클래스. 주로 디버깅 용도의 static 메쏘드를 모아놓음.
 * 
 * @author cwsong
 * @version 2010. 9. 13.
 */
public class DebugUtil {

	/**
	 * 변수로 전달된 임의의 도메인객체, VO객체의 멤버변수들을 콘솔에 출력한다.
	 * 
	 * @param object
	 * @throws Exception
	 * 
	 * @author cwsong
	 */
	public static void objectInfo(Object object) {
		try {
			synchronized (new Object()) {
				if (object == null) {
					System.out.println("-----> [ERROR] passed object is NULL!!");
					return;
				}
				Class clazz = object.getClass();
				if (clazz.isPrimitive()) {
					System.out.println("-----> [ERROR] passed object is PRIMITIVE type!!");
					return;
				}
				Field[] fields = clazz.getDeclaredFields();
				System.out.println("|---------------> [START] debugging [ object of " + object.getClass().getName()
						+ " ] total member count : " + fields.length + " <---------------");

				Object _object = null;
				Class _clazz = null;
				int longestNameLength = 0, longestClazzNameLength = 0;
				// int longestClazzLength = 0;
				String shortClazzName = "";
				String[] clazzNames = new String[fields.length];
				int interval = 0, interval2 = 0;
				String glue = "", glue2 = "";

				for (int i = 0; i < fields.length; i++) {
					if (fields[i].getName().length() > longestNameLength) {
						longestNameLength = fields[i].getName().length();
					}
					if (fields[i].getType().getName().indexOf("[") != -1) {
						shortClazzName = "Arrays";
					} else {
						shortClazzName = fields[i].getType().getName().split("\\.")[fields[i].getType().getName().split("\\.").length - 1];
					}
					clazzNames[i] = shortClazzName;
				}

				for (int i = 0; i < clazzNames.length; i++) {
					if (clazzNames[i].length() > longestClazzNameLength) {
						longestClazzNameLength = clazzNames[i].length();
					}
				}

				for (int i = 0; i < fields.length; i++) {
					glue = "";
					glue2 = "";
					// 멤버변수는 private이므로 접근 가능하도록 수정한다.
					if (!fields[i].isAccessible()) {
						fields[i].setAccessible(true);
					}
					_object = fields[i].get(object);
					_clazz = fields[i].getType();
					interval = longestNameLength - fields[i].getName().length();
					if (_clazz.getName().indexOf("[") == -1) {
						interval2 = longestClazzNameLength
								- _clazz.getName().split("\\.")[_clazz.getName().split("\\.").length - 1].length();
					} else {
						interval2 = longestClazzNameLength - 6;
					}
					for (int j = 0; j < interval; j++) {
						glue += " ";
					}
					for (int j = 0; j < interval2; j++) {
						glue2 += " ";
					}
					if (i < 10) {
						System.out.println("|> " + i + "  " + clazzNames[i] + glue2 + " " + fields[i].getName() + glue + " - " + _object);
					} else {
						System.out.println("|> " + i + " " + clazzNames[i] + glue2 + " " + fields[i].getName() + glue + " - " + _object);
					}
				}
				System.out.println("|---------------> [END] debugging [ object of " + object.getClass().getName()
						+ " ] <-----------------------------------------");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 리스트 객체의 Info 출력
	 * 
	 * @param objectList
	 */
	public static void listInfo(List objectList) {
		for (int i = 0; i < objectList.size(); i++) {
			System.out.println(" ------------------> No." + i + " ListObject ---------------------------");
			objectInfo(objectList.get(i));
		}
	}
}
